const {
	db,
	dbCmd,
	result_success,
	result_error,
	appletInfoCollection,
	collectionCollection,
	userCollection
} = require('../../common/constants')
module.exports = async function(params = {}) {
	const token = this.getUniIdToken()
	if (!token) {
		return result_error('登录失效,请重新登录')
	}
	let payload = await this.uniID.checkToken(token)
	if (payload.code) {
		return payload
	}
	let cUser = await userCollection.doc(payload.uid).get()
	cUser = cUser.data[0]
	//获取当前应用
	let appletInfoData = await appletInfoCollection.doc(cUser.app_id).get()
	appletInfoData = appletInfoData.data[0]
	let app_id = appletInfoData._id
	let _id = params._id
	if (!_id) {
		return result_error('_id为空')
	}
	let collectionDatas = params.collectionDatas
	if (collectionDatas && collectionDatas.length > 0) {
		await collectionCollection.where({
			app_id: app_id,
			shop_id: app_id,
			status: '进行中'
		}).update({
			checked: false
		})
	}
	let collectionData = []
	for (var i in collectionDatas) {
		if (collectionDatas[i].checked) {
			await collectionCollection.doc(collectionDatas[i]._id).update({
				checked: true
			})
			collectionData.push(collectionDatas[i])
		}
	}
	await db.collection('wmzmall-quick-collection').doc(_id).update({
		vip: params.vip,
		status: params.status,
		recharge: params.recharge,
		collection: collectionData
	})
	return result_success()
}